var midiMin = 48;
var midiMax = 72;
var maxFreq = 2400;
var posA = 1/6 * maxFreq;
var posB = 2/6 * maxFreq;
var posC = 3/6 * maxFreq;
var posD = 4/6 * maxFreq;
var posE = 5/6 * maxFreq;
var posF = 6/6 * maxFreq;

function valueToMidi(value, minValue, maxValue){
	var midiMin = 48;
	var midiMax = 72;
	//Possibly round this value.
	//alert("midiMin: " + midiMin + ", midiMax: " + midiMax + ", value: " + value + ", minValue: " + minValue + ", maxValue: " + maxValue); 
	//return (value-minValue)/(maxValue-minValue)*(midiMax-midiMin))+midiMin;
	return (value-minValue)/(maxValue-minValue)*(midiMax-midiMin)+midiMin;
	//return 2;
}

function midiToFrequency(midi){
	//alert(midi);
	return 13.75 * Math.pow(2, (midi-9)/12);
}

function frequencyToRed(frequency) {
	var red;
	//RED
	if(frequency<=posA){
	//INCREASE
		red = (frequency/posA);
	}
	
	else if(frequency>posA && frequency<=posB){
		red = 1-(frequency-posA)/(posB-posA);
	}
	
	else if(frequency>posC && frequency<=posD){
		red = 1-(posD-frequency)/(posD-posC);
	}
	
	else if(frequency>posD){
		red = 1;
	}
	
	else{
		red = 0;
	}
	
	return red;
}

function frequencyToGreen(frequency){
	var green;
	//ORANGE
	if(frequency> posB && frequency<=posC){
		green = 1-(posC-frequency)/(posC-posB);
	}
	
	else if(frequency>posC &&frequency<=posD){
		green = 1;
	}
	else if(frequency> posD && frequency <=posE){
		green = 1-0.35*((frequency-posD)/(posE-posD));
	}
	
	else if(frequency>posE && frequency<=posF){
		green = 0.65-0.65*((frequency-posE)/(posF-posE));
	}
	
	
	else{
		green = 0;
	}

	return green;
}

function frequencyToBlue(frequency){
	var blue;
	//BLUE
	if(frequency<=posA){
		blue = (frequency/posA);
	}
	else if(frequency> posA && frequency<=posB){
		blue = 1;
	}
	
	else if(frequency>posB && frequency<=posC){
		blue = 1-(frequency-posB)/(posC-posB);
	}
	
	else{
		blue = 0;
	}

	return blue;
}



